package jp.co.systemi.asp.workflow.mail;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Logger;

import jp.co.systemi.asp.utils.LazyLoadingList;
import jp.co.systemi.asp.workflow.Main;
import jp.co.systemi.asp.workflow.data.Reminder;
import jp.co.systemi.asp.workflow.data.ReminderMeta;
import jp.co.systemi.asp.workflow.peer.ReminderPeer;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.slim3.datastore.Datastore;
import org.slim3.datastore.GlobalTransaction;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class ReminderList extends Main {
    private static final Logger log = Logger.getLogger(ReminderList.class
            .getName());
    int rowPerPage = 10;
    PagingNavigator wi_navi = null;
    LazyLoadingList<Reminder> reminders = new LazyLoadingList<Reminder>() {
        private static final long serialVersionUID = 7141859951545473818L;

        @Override
        public int size() {
            return ReminderPeer.getSize();
        }

        @Override
        public int getCacheSize() {
            return rowPerPage;
        }

        @Override
        public List<Reminder> getPage(int start, int size) {
            return ReminderPeer.getPage(start, size);
        }
    };

    public ReminderList() {
        String dateTimeFormat = getString("datetime.format");
//      private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        final SimpleDateFormat sdf = new SimpleDateFormat(dateTimeFormat);
        try {
            rowPerPage = Integer.parseInt(getString("rows.per.page"));
        } catch (Exception e) {
            log.info(e.getLocalizedMessage());
        }
        final WebMarkupContainer tblReminders = new WebMarkupContainer(
                "wi_tblReminders");
        tblReminders.setOutputMarkupId(true);

        final PageableListView<Reminder> listview = new PageableListView<Reminder>(
                "wi_trReminder", reminders, rowPerPage) {
            private static final long serialVersionUID = 8901173915946652261L;

            protected void populateItem(ListItem<Reminder> item) {
                UserService userService = UserServiceFactory.getUserService();
                com.google.appengine.api.users.User usr = userService
                        .getCurrentUser();
                String loginId = usr.getEmail();
                final Reminder reminder = (Reminder) item.getModelObject();
                String oddEven = (reminder.getSeq() % 2 == 0) ? "even" : "odd";
                log.info("seq : " + reminder.getSeq());
                item.add(new SimpleAttributeModifier("class", oddEven));

                item.add(new Label("wi_tdReminderDateTime", sdf.format(reminder.getReminderDate())));
                item.add(new Label("wi_tdSubject", reminder.getSubject()));

                AjaxLink<Object> wi_delete = new AjaxLink<Object>("wi_tdDelete") {
                    private static final long serialVersionUID = 4414373446594913835L;

                    @Override
                    public void onClick(AjaxRequestTarget target) {
                        GlobalTransaction gtx = Datastore
                                .beginGlobalTransaction();
                        ReminderMeta rm = ReminderMeta.get();

                        gtx.commit();

                        target.addComponent(tblReminders);
                    }
                };
                Image image = new Image("wi_deleteImg");
                image.add(new SimpleAttributeModifier("src", "/images/delete_icon.jpg"));
                wi_delete.add(image);

                item.add(wi_delete);
            }
        };

        wi_navi = new PagingNavigator("wi_navi", listview);
        Form<Object> form = new MainForm<Object>(this,
                "wi_reminderListForm") {
            private static final long serialVersionUID = 1L;
            /**
             * 更新ボタン
             */
            @Override
            protected void onSubmitInner() {
                reminders.clear();
                listview.setCurrentPage(0);
            }
        };

        tblReminders.add(listview);
        tblReminders.add(wi_navi);
        form.add(tblReminders);
    }
}
